﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Windows.Forms;
using QLTVNEW.Properties;
using DevComponents.DotNetBar;
namespace QLTVNEW
{
    public partial class frmNXB : DevComponents.DotNetBar.Office2007Form
    {
        public frmNXB()
        {
            InitializeComponent();
        }

        #region 'LT'
        void txtReadOnly(bool a)
        {
            txtMaNXB.ReadOnly=a;
            txtTenNXB.ReadOnly=a;
            txtDiaChi.ReadOnly=a;
           
        }

        void btnThem(bool a)
        {
            btnupdate.Enabled=a;
            btndel.Enabled=a;
        }

        void btnSua(bool a)
        {
            btninsert.Enabled=a;
            btndel.Enabled=a;
        }

        void LoadGridView()
        {
            var query = from nxb in db.NHAXUATBANs select nxb;
            dataGridViewX1.DataSource = query;
        }
        #endregion

        QLTVDataContext db = new QLTVDataContext();
        private void frmNXB_Load(object sender, EventArgs e)
        {
            LoadGridView();
            txtMaNXB.Text = dataGridViewX1.Rows[0].Cells[0].Value.ToString().Trim();
            txtTenNXB.Text = dataGridViewX1.Rows[0].Cells[1].Value.ToString().Trim();
      
        }

        private void frmNXB_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.AutoValidate = AutoValidate.Disable;
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            superTooltip1.SetSuperTooltip(btncancel, new SuperTooltipInfo("Hủy Bỏ.", "", "", null, null, eTooltipColor.Lemon));
            btnsave.Enabled = true;
            txtReadOnly(false);
            txtMaNXB.ReadOnly = true;
            btnSua(false);
            btnupdate.Image = Resources.Cancel;
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtReadOnly(false);
            foreach (Control ctr in groupBox1.Controls)
            {
                if (ctr is TextBox)
                {
                    ctr.Text = "";
                    labelX1.Focus();
                }
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            if (btnsave.Enabled == true)
            {
                txtReadOnly(true);
                btnSua(true);
                btnThem(true);
                btninsert.Image = Resources.add1_;
                btnupdate.Image = Resources.edit;
                btnsave.Enabled = false;
            }
            else
            {
                this.AutoValidate = AutoValidate.Disable;
                this.Close();
            }
        }

        private void btninsert_Click(object sender, EventArgs e)
        {
            superTooltip1.SetSuperTooltip(btncancel, new SuperTooltipInfo("Hủy Bỏ.", "", "", null, null, eTooltipColor.Lemon));
            txtMaNXB.ReadOnly = true;
            txtTenNXB.Focus();
            btnThem(false);
            btnsave.Enabled = true;
            txtReadOnly(false);
            btninsert.Image = Resources.Cancel;
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            if (txtMaNXB.Text!="")
            {

            }
            else
                MessageBox.Show("Chưa Nhập Mã Nhà Xuất Bản","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (btnupdate.Enabled == false)
            {
                string err = "";
                db.uspCRUDNXB('t', 0, txtTenNXB.Text, txtDiaChi.Text, ref err);
                MessageBox.Show(err.ToString());
                btnThem(true);
                txtReadOnly(true);
                btninsert.Image = Resources.add1_;
                btnsave.Enabled = false;
                LoadGridView();
            }
            if (btninsert.Enabled==false)
            {
                if (txtMaNXB.Text !="")
                {
                    NHAXUATBAN nxb = db.NHAXUATBANs.Where(n => n.MaNXB == int.Parse(txtMaNXB.Text)).SingleOrDefault<NHAXUATBAN>();
                    if (nxb!=null)
                    {
                        db.Connection.Open();
                        db.Transaction=db.Connection.BeginTransaction();
                        try
                        {
                            nxb.TenNXB = txtTenNXB.Text.Trim();
                            nxb.GhiChu = txtDiaChi.Text.Trim();
                            db.SubmitChanges();
                            db.Transaction.Commit();
                            MessageBox.Show("Sửa Thành Công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                            LoadGridView();
                        }
                        catch (System.Exception ex)
                        {
                            db.Transaction.Rollback();
                            MessageBox.Show(ex.Message,"Sửa Thất Bại",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        }
                        finally
                        {
                            if (db.Connection.State == ConnectionState.Open)
                                db.Connection.Close();
                            db.Transaction = null;
                            btnupdate.Image = Resources.Cancel;
                            btnSua(true);
                            txtReadOnly(true);
                            btnsave.Enabled = false;
                        }

                    }
                }
            }
        }
        int dong;
        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dong = e.RowIndex;
                txtMaNXB.Text = dataGridViewX1.Rows[dong].Cells[0].Value.ToString().Trim();
                txtTenNXB.Text = dataGridViewX1.Rows[dong].Cells[1].Value.ToString().Trim();
                txtDiaChi.Text = dataGridViewX1.Rows[dong].Cells[2].Value.ToString().Trim();
            }
            catch
            {
                return;
            }
        }
    }
}
